package forList;

public class NodeTest {

    public static void main(String[] args) {

        NodeA first = new NodeA();
        NodeA last = new NodeA();

        NodeA jack = new NodeA();
        jack.name = "jack";

        NodeA hanj = new NodeA();
        hanj.name = "hanj";

        NodeA marry = new NodeA();
        marry.name = "marry";

        NodeA lucy = new NodeA();
        lucy.name = "lucy";

        jack.next = hanj;
        hanj.next = marry;
        marry.next = lucy;

        lucy.prev = marry;
        marry.prev = hanj;
        hanj.prev = jack;

        // 添加一个数据
        NodeA bob = new NodeA();
        bob.name = "bob";
        jack.next = bob;
        bob.next = hanj;
        hanj.prev = bob;
        bob.prev = jack;

        first = jack;
        last = lucy;

        // 遍历输出双向链表的数据(正向)
        while (first != null) {

            System.out.println(first.name);
            first = first.next;

        }

        System.out.println("------------------------------------");

        // 遍历输出双向链表的数据(反向)
        while (last != null) {

            System.out.println(last.name);
            last = last.prev;

        }
    }

}

class NodeA {

    String name;
    NodeA prev;
    NodeA next;

    @Override
    public String toString() {

        return name;
    }

}
